$(function(){
    $('#login').on('click',function(){
        $('.login1').hide()
        $('.reg').show()
    })
    $('#reg').on('click',function(){
        $('.reg').hide()
        $('.login1').show()
    })

    //自定义校验
    layui.form.verify({
        pwd:[/^[\S]{6,16}$/,'密码必须为6~16位不为空字符'],
        repwd(value){
        let pwd = $('#pwd1').val().trim()
        if(pwd !== value) return '两次密码不一致!'
        }
    
    })
    //注册页面提交验证
    $('.reg>form.layui-form').on('submit',function(e){
        e.preventDefault()
        let fd = $(this).serialize()
        $.ajax({
            method:'post',
            url:'/api/reguser',
            data:fd,
            success:function(e){
                if(e.status !== 0) return layui.layer.msg(e.message)
                layui.layer.msg(e.message)
                $('#reg').click()
            }
        })
    })
    //登录页面提交验证
    $('.login1>form.layui-form').on('submit',function(e){
        e.preventDefault()
        let fd1 = $(this).serialize()
        $.ajax({
            method:'post',
            url:'/api/login',
            data:fd1,
            success:function(e){
                if(e.status !== 0) return layui.layer.msg(e.message)
                layui.layer.msg(e.message)
                sessionStorage.setItem('token',e.token)
                location.href = "/index.html"
            }
        })
    })


})